package org.jeecg.modules.demo.yyc.reportdaily.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.*;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.modules.demo.yyc.reportdaily.entity.PlatformDaily;
import org.jeecg.modules.demo.yyc.reportdaily.entity.Report;
import org.jeecg.modules.demo.yyc.reportdaily.service.IPlatformDailyService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 运营日报
 * @Author: jeecg-boot
 * @Date:   2022-11-01
 * @Version: V1.0
 */
@Api(tags="运营日报")
@RestController
@RequestMapping("/yyc.dailyreport/platformDaily")
@Slf4j
public class PlatformDailyController extends JeecgController<PlatformDaily, IPlatformDailyService> {
	@Autowired
	private IPlatformDailyService platformDailyService;

	/**
	 * 分页列表查询
	 *
	 * @param platformId
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "运营日报-分页列表查询")
	@ApiOperation(value="运营日报-分页列表查询", notes="运营日报-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent = "yyc/dailyreport/ReportList")
	public Result<Report> queryPageList(String platformId,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<HashMap<String,?>> page = new Page<>(pageNo, pageSize);
		Report report = platformDailyService.getReports(page,platformId);
		return Result.OK(report);
	}

	 /**
	  * 导入日报
	  * @param file
	  * @param request
	  * @return
	  */
	 @PostMapping("/uploadImport")
	 public @ResponseBody Result uploadImport(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		 platformDailyService.importExcel(file);
		 return Result.OK("导入成功");
	 }

	 /**
	  * 下载模板，用于导入
	  * @param platformId 平台id
	  */
	 @GetMapping("/exportTemplate")
	 public void exportTemplate(String platformId, HttpServletResponse response) throws IOException {
		 ServletOutputStream out;
		 try(Workbook wb = platformDailyService.getTemplate(platformId)){
			 response.setHeader("content-disposition", "attachment;filename=导出的.xls");
			 out = response.getOutputStream();
			 wb.write(out);
		 }
		 out.flush();
	 }

	@GetMapping(value = "/export")
	 @PermissionData(pageComponent = "yyc/dailyreport/ReportList")
	public void export(String platformId,
					   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
					   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
					   HttpServletResponse response) throws IOException {
		 Page<HashMap<String,?>> page = new Page<>(pageNo, pageSize);
		 Report report = platformDailyService.getReports(page,platformId);

		 List<ExcelExportEntity> entityList = new ArrayList<>();

		 if(page.getRecords().size() > 0){
			 entityList.add(new ExcelExportEntity("平台","平台"));
			 entityList.add(new ExcelExportEntity("店铺","店铺"));
			 entityList.add(new ExcelExportEntity("商品","商品"));
			 entityList.add(new ExcelExportEntity("时间","create_time"));
		 }

		 report.getDailyItems().forEach(item -> {
			 entityList.add(new ExcelExportEntity(item.getName(),item.getName()));
		 });

		 ServletOutputStream out;
		 try (Workbook wb = ExcelExportUtil.exportExcel(new ExportParams(null, "sheetName"), entityList, report.getRecords().getRecords())) {

			 response.setHeader("content-disposition", "attachment;filename=导出的.xls");
			 out = response.getOutputStream();
			 wb.write(out);
		 }
		 out.flush();
	}

	/**
	 *   添加
	 *
	 * @param platformDailys
	 * @return
	 */
	@AutoLog(value = "运营日报-添加单个日报项")
	@ApiOperation(value="运营日报-添加", notes="运营日报-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:platform_daily:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody List<PlatformDaily> platformDailys) {
		platformDailyService.saveEntireDaily(platformDailys);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param platformDaily
	 * @return
	 */
	@AutoLog(value = "运营日报-编辑")
	@ApiOperation(value="运营日报-编辑", notes="运营日报-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:platform_daily:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody PlatformDaily platformDaily) {
		platformDailyService.updateById(platformDaily);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "运营日报-通过id删除")
	@ApiOperation(value="运营日报-通过id删除", notes="运营日报-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:platform_daily:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		platformDailyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "运营日报-批量删除")
	@ApiOperation(value="运营日报-批量删除", notes="运营日报-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:platform_daily:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.platformDailyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "运营日报-通过id查询")
	@ApiOperation(value="运营日报-通过id查询", notes="运营日报-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<PlatformDaily> queryById(@RequestParam(name="id",required=true) String id) {
		PlatformDaily platformDaily = platformDailyService.getById(id);
		if(platformDaily==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(platformDaily);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param platformDaily
    */
    //@RequiresPermissions("org.jeecg.modules.demo:platform_daily:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PlatformDaily platformDaily) {
        return super.exportXls(request, platformDaily, PlatformDaily.class, "运营日报");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("platform_daily:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PlatformDaily.class);
    }

}
